const webDataUtil = require('../../util/webDataUtil')
const reqlib = require('app-root-path').require
const btbAs = reqlib('/app/common/btb')
const commonDataCreate = require('./utils/commonDataCreate')
const AnalysisInfoFlag = reqlib('/app/analysis/analysisInfoFlag')

const getWithKlineList = async (item, interval) => {
  const key = item.code
  item.symbol = `${item.code}USDT`
  item.min_qty = item.f_min_qty
  item.max_qty = item.f_max_qty
  item.step_size = item.f_step_size
  item.min_notional = item.f_min_notional
  item.tick_size = item.f_tick_size
  item.klineList = await webDataUtil.getFuturesKLinesAll({
    name: `${key}`,
    interval: interval,
    count: 500
  })
}

const analysisBtbItem = (item, interval) => {
// 慢是因为分析2000天的，10个就20000天
  item.analysisInfoList = btbAs.getItemInfoList(item.klineList)
  // 都转为从近到远
  item.analysisInfoList.reverse()
  item.analysisInfoList.forEach((v, index) => {
    commonDataCreate.asInfoExtend(item.analysisInfoList, v, index)
  })
  item.klineList.reverse()
  item.analysisInfoFlag = new AnalysisInfoFlag({
    analysisInfoList: item.analysisInfoList,
    type: interval,
    code: item.code
  })
}

module.exports = async function (params) {
  // let btbList = await BtbIndex.findWidthField({
  // }, {
  //   countResultInfo: 0,
  //   monthEmaInfo: 0,
  //   weekEmaInfo: 0,
  //   dayEmaInfo: 0,
  //   hour4EmaInfo: 0,
  //   hourEmaInfo: 0,
  //   minute15EmaInfo: 0
  // }, {
  //   sort: '-proportion'
  // })
  // for (let i=0;i<btbList.length;i++) {
  //   await BtbIndex.update({
  //     code: btbList[i].code
  //   }, {
  //     manual: 0
  //   })
  // }
  const item = {
    code: params.code
  }
  await getWithKlineList(item, params.interval)
  analysisBtbItem(item, params.interval)
  return {
    code: item.code,
    interval: params.interval,
    ...item.analysisInfoFlag.flagInfo,
    analysisInfoList: []
  }
}
